﻿using SCG.General;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TTEngine.Rooms
{
    public class Prison : PatternRoom
    {
        //constructor
        public Prison()
            : base()
        {
            FactionTags.Add("Builder");
        }

        //clone
        public override Room Clone()
        {
            return new Prison();
        }

        //room shape
        public override string PatternMap
        {
            get
            {
                return GeneratePatternMap(6, 8);
            }
        }

        //place some prison cells
        public override void PostProcessing()
        {
            base.PostProcessing();

            List<SCGPoint> usedPoints = new List<SCGPoint>();

            //this key will unlock the cells
            Key key = new Key();

            CharMatrix pattern = new CharMatrix("XXX,XXX,---,---");
            int twists = 0;
            for (int cells = 0; cells < 3; cells++)
            {
                SCGPoint pt = Level.TerrainMap.FindPattern(pattern, CenterX, CenterY, 8, ref twists);
                if (pt != null && !usedPoints.ContainsPoint(pt))
                {
                    usedPoints.Add(pt);

                    //modify the terrain
                    CharMatrix jail = new CharMatrix("XXX,-X-,-X-,---");
                    for (int nt = 0; nt < twists; nt++)
                        jail = jail.Twist();
                    for (int fy = 0; fy < jail.Height; fy++)
                        for (int fx = 0; fx < jail.Width; fx++)
                            Level.TerrainMap.Set(pt.X + fx, pt.Y + fy, jail.Get(fx, fy));

                    //determine offsets
                    int bx = 0;
                    int by = 0;
                    int mx = 0;
                    int my = 0;
                    switch (twists)
                    {
                        case 0:
                            {
                                bx = 1;
                                by = 1;
                                mx = 1;
                                my = 2;
                                break;
                            }
                        case 1:
                            {
                                bx = 2;
                                by = 1;
                                mx = 1;
                                my = 1;
                                break;
                            }
                        case 2:
                            {
                                bx = 1;
                                by = 2;
                                mx = 1;
                                my = 1;
                                break;
                            }
                        default:
                            {
                                bx = 1;
                                by = 1;
                                mx = 2;
                                my = 1;
                                break;
                            }
                    }

                    //the prisoner
                    Prisoner p = new Prisoner();
                    Level.AddObjectToMap(p, pt.X + mx, pt.Y + my);

                    //place the cell bars
                    Bars bars = new Bars();
                    bars.Lock.Code = key.Code;
                    Level.AddObjectToMap(bars, pt.X + bx, pt.Y + by);
                }
            }

            //spawn guards
            List<Actor> guards = Faction.SpawnMonsters(Level, CenterX, CenterY);
            if (guards.Count > 0)
            {
                Actor guard = guards[0];
                guard.AddGear(key);
            }
        }
    }
}
